/*
 * Proto Streamer
 * Copyright (C) 2009 John Pritchard.
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License as
 * published by the Free Software Foundation; either version 2 of the
 * License, or (at your option) any later version.
 * 
 * This program is distributed in the hope that it will be useful, but
 * WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
 * 02110-1301 USA.
 */
package p.s;

import java.io.CharArrayWriter;
import java.io.IOException;
import java.nio.charset.Charset;

/**
 * A subclass may fill the content buffer from its 'init' method.  The
 * subclass defines the response content type and length in its 'init'
 * method.
 * @author jdp
 */
public abstract class Buffer
    extends Abstract
{
    public final static Charset UTF = Charset.forName("UTF-8");

    public final static String CRLF = "\r\n";



    protected final CharArrayWriter buffer;

    public Buffer(){
        super();
        this.buffer = new CharArrayWriter();
    }


    protected void print(String s){
        if (null != s)
            this.buffer.append(s);
    }
    protected void println(String s){
        if (null != s)
            this.buffer.append(s);
        this.buffer.append(CRLF);
    }
    protected void println(){
        this.buffer.append(CRLF);
    }
    public void tail(Request request, Output out)
        throws IOException
    {
        byte[] content = this.buffer.toString().getBytes(UTF);
        if (null != content)
            out.write(content,0,content.length);
    }
}
